<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :before-close="cancel"
             :visible.sync="visible" append-to-body width="1020px">
    <el-form ref="dataForm" :inline="true" :model="dataForm" :rules="dataRule" size="small"
             @keyup.enter.native="dataFormSubmit()" label-width="150px">

      <el-form-item label="dataRuleId" prop="dataRuleId">
        <el-input v-model="dataForm.dataRuleId" placeholder="" style="width: 245px;"></el-input>
      </el-form-item>
      <el-form-item label="dataRuleName" prop="dataRuleName">
        <el-input v-model="dataForm.dataRuleName" placeholder="" style="width: 245px;"></el-input>
      </el-form-item>
      <el-form-item label="dataRuleMysqlType" prop="dataRuleMysqlType">
        <el-input v-model="dataForm.dataRuleMysqlType" placeholder="" style="width: 245px;"></el-input>
      </el-form-item>
      <el-form-item label="dataRuleJavaType" prop="dataRuleJavaType">
        <el-input v-model="dataForm.dataRuleJavaType" placeholder="" style="width: 245px;"></el-input>
      </el-form-item>
      <el-form-item label="dataRuleDescription" prop="dataRuleDescription">
        <el-input v-model="dataForm.dataRuleDescription" placeholder="" style="width: 245px;"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {
        queryGeneratorDataByCondition,
        updateGeneratorData,
        saveGeneratorDataSingleLineData
    } from '@/api/GeneratorData.js'

    export default {
        data() {
            return {
                visible: false,
                loading: false,
                dataForm: {
                    dataRuleId: '',
                    dataRuleName: '',
                    dataRuleMysqlType: '',
                    dataRuleJavaType: '',
                    dataRuleDescription: ''
                },
                dataRule: {
                    dataRuleId: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ], dataRuleName: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ], dataRuleMysqlType: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ], dataRuleJavaType: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ], dataRuleDescription: [
                        {required: true, message: '不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(id) {
                console.log(id)
                this.dataForm.id = id || 0
                this.visible = true
                if (this.dataForm.id) {
                    queryGeneratorDataByCondition({id: this.dataForm.id}).then(res => {
                        if (res.hasError) {
                            this.$notify({
                                title: res.error.message,
                                type: 'fail',
                                duration: 2500
                            })
                        } else {
                            if (res.data == '') {
                                res.data = {}
                            }
                            this.dataForm = res.data;
                        }
                    }).catch(err => {
                        this.loading = false
                        console.log(err.response.data.message)
                    })
                }
            },
            cancel() {
                this.resetForm()
            },
            resetForm() {
                this.visible = false
                this.$refs['dataForm'].resetFields()
            },
            // 表单提交
            dataFormSubmit() {
                let id = this.dataForm.id
                console.log(id)
                let saveParam = {
                    dataRuleId: this.dataForm.dataRuleId,
                    dataRuleName: this.dataForm.dataRuleName,
                    dataRuleMysqlType: this.dataForm.dataRuleMysqlType,
                    dataRuleJavaType: this.dataForm.dataRuleJavaType,
                    dataRuleDescription: this.dataForm.dataRuleDescription
                }
                if (id) {
                    console.log(id)
                    this.doUpdate(saveParam);
                } else {
                    console.log(id)
                    this.doSave(saveParam);
                }
            },
            doSave(saveParam) {
                saveGeneratorDataSingleLineData(saveParam).then(res => {
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        this.$notify({
                            title: res.data,
                            type: 'success',
                            duration: 2500
                        })
                    }
                }).catch(err => {
                    this.loading = false
                    console.log(err.response.data.message)
                })
            },
            doUpdate(saveParam) {
                updateGeneratorData(saveParam).then(res => {
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        this.$notify({
                            title: res.data,
                            type: 'success',
                            duration: 2500
                        })
                    }
                }).catch(err => {
                    this.loading = false
                    console.log(err.response.data.message)
                })
            },
        }
    }
</script>
